<?php
namespace scenic\controllers;

use Yii;
use yii\web\Controller;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;
use scenic\models\ScenicDetail;
use scenic\models\ScenicRole;

header('Content-type:text/html;charset=utf-8');

class DetailController extends Controller {


	public function init(){
		$view = Yii::$app->view;
		$view->params['layout_data'] = '使用详情';
	}

	public function actionIndex(){
		
		$model = new ScenicDetail();
		$query = ScenicDetail::find();
		$pages = new Pagination(['totalCount'=>$query->count(),'pageSize'=>10]);

		$model = $query->offset($pages->offset)->limit($pages->limit)->orderBy(['id'=>SORT_ASC])->all();
		$scenic = ScenicRole::find()->all();
		$pagetotal = '';
		foreach($model as $k=>$v){
			$scenicid = $v['scenic_id']-1;
			$v['scenic_id'] = $scenic[$scenicid]['scenic_name'];
			$v['rake'] = $scenic[$scenicid]['rake'];
			$pagetotal += $v['rake'];
		}
		$sum = $this->amount('');

		return $this->render('index',['model'=>$model,'total'=> $sum,'pagetotal'=>$pagetotal,'scenic'=>$scenic,'pages'=>$pages]);
	}

	public function amount($scenicid){
		$model = new ScenicDetail();
		if(!empty($scenicid)){
			$count = ScenicDetail::find()->where(['scenic_id'=>$scenicid])->count();
			$scenic = ScenicRole::find()->where(['id'=>$scenicid])->one();
			$rake = $scenic->rake;
			$sum = $count*$rake;
			return $sum;
		} else {
			$detail = $model->find()->all();
			$total = '';
			foreach($detail as $k=>$v){
				$scenicname = ScenicRole::findOne($v['scenic_id']);
				$v['rake'] = $scenicname['rake'];
				$total += $v['rake'];
			}
			return $total;
		}
	}

	public function actionSel(){
		$model = new ScenicDetail();
		if(Yii::$app->request->isAjax){
			//scenic_id 景区id
			$scenicid = !empty($_POST['scenicid']) ? $_POST['scenicid'] : '';
			$re = ScenicDetail::find()->where(['scenic_id'=>$scenicid])->asArray()->all();
		}
		$result= [
			'data' =>$re,
		];
		$data = json_encode($result);
		return $data;

	}
}